package com.wjyf.reducing_bento.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wjyf.reducing_bento.common.ErrorCode;
import com.wjyf.reducing_bento.model.dto.CouponAddDto;
import com.wjyf.reducing_bento.model.entity.CouponBean;
import com.wjyf.reducing_bento.model.entity.CouponReceiveBean;
import com.wjyf.reducing_bento.model.entity.UserBean;
import com.wjyf.reducing_bento.exception.BusinessException;
import com.wjyf.reducing_bento.mapper.CouponMapper;
import com.wjyf.reducing_bento.service.CouponReceiveService;
import com.wjyf.reducing_bento.service.CouponService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjyf.reducing_bento.service.UserService;
import com.wjyf.reducing_bento.model.vo.CouponVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.wjyf.reducing_bento.constant.CouponConstant.*;
import static com.wjyf.reducing_bento.constant.CouponReceiveConstant.NOT_USED;

/**
 * <p>
 * 优惠券基础配置表 服务实现类
 * </p>
 *
 * @author wjyf
 * @since 2023-03-14 08:36:32
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, CouponBean> implements CouponService {

    @Resource
    private UserService userService;

    @Resource
    private CouponReceiveService couponReceiveService;

    @Override
    public boolean add(CouponAddDto couponAddDto) {
        if (couponAddDto == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Long cpnum = couponAddDto.getCpnum();
        String name = couponAddDto.getName();
        Date startTime = couponAddDto.getStartTime();
        Date endTime = couponAddDto.getEndTime();
        Double money = couponAddDto.getMoney();
        String remarks = couponAddDto.getRemarks();
        Date nowDate = new Date();
        if (cpnum <= 0 ||
                StringUtils.isAnyBlank(name, remarks) ||
                //startTime.before(nowDate) ||
                endTime.before(nowDate) ||
                startTime.after(endTime) ||
                money <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        CouponBean couponBean = new CouponBean();
        BeanUtil.copyProperties(couponAddDto, couponBean);
        couponBean.setCreateTime(nowDate)
                .setStatus(nowDate.before(startTime) ? NOT_STARTED : HOLDING)
                .setType(FULL_DECREMENT_TYPE);
        boolean save = save(couponBean);
        if (!save) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        return save;
    }

    @Override
    public Page<CouponBean> get(Long current, Long pageSize) {
        if (current == null || pageSize == null || current <= 0 || pageSize <= 0){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Page<CouponBean> page = page(new Page<CouponBean>(current, pageSize),
                new LambdaQueryWrapper<CouponBean>().orderByDesc(CouponBean::getStatus));
        return page;
    }

    @Override
    public List<CouponVo> getOneself(HttpServletRequest request) {
        UserBean user = userService.getUser(request);
        List<CouponReceiveBean> list = couponReceiveService.list(new LambdaQueryWrapper<CouponReceiveBean>()
                .eq(CouponReceiveBean::getUId, user.getUId())
                .eq(CouponReceiveBean::getStatus, NOT_USED));
        List<CouponVo> res = new ArrayList<>();
        for (CouponReceiveBean couponReceiveBean : list) {
            CouponBean couponBean = getById(couponReceiveBean.getCouponId());
            CouponVo couponVo = new CouponVo();
            couponVo.setId(couponReceiveBean.getId());
            couponVo.setCouponId(couponReceiveBean.getCouponId());
            couponVo.setName(couponBean.getName());
            couponVo.setCouponMoney(couponBean.getMoney());
            res.add(couponVo);
        }
        return res;
    }
}
